function onSuccessGeoLocation(latitude, longitude) {
	/* geocoder is being user to know the address, using latlng */
	var geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(latitude, longitude);
	$("form input[name='latitude']").val(latitude);
	$("form input[name='longitude']").val(longitude);

	geocoder.geocode({
		'latLng' : latlng
	}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			if (results[1]) {
				$("#location-reverse-geocode").html(
						results[1].formatted_address);
			} else {
				alert('No results found');
			}
		} else {
			alert('Geocoder failed due to: ' + status);
		}
	});
}

/**
 * map and location code
 */
function doMapThings() {
	if (window.location.hash) {
		var latlng = window.location.hash.substring(2).split(",");
		onSuccessGeoLocation(+latlng[0], +latlng[1]);
	} else {
		if ("geolocation" in navigator) {
			var geo_options = {
				enableHighAccuracy : true,
				maximumAge : 30000,
				timeout : 27000
			};
			navigator.geolocation.getCurrentPosition(
					function(pos) {
						onSuccessGeoLocation(pos.coords.latitude,
								pos.coords.longitude);
					}, null, geo_options);
		} else {
			/* geolocation IS NOT available */
		}

	}

}

function doFormThings() {
	/* generate random false sensor data */
	$.ajax({
		type : "GET",
		url : "observation?current=true",
		dataType : "json"
	}).done(function(json) {
		$("input[name='co']").val(json.co);
		$("input[name='no2']").val(json.no2);
		$("input[name='so2']").val(json.so2);
		$("input[name='o3']").val(json.o3);
		$("input[name='pm10']").val(json.pm10);
		$("input[name='pm2p5']").val(json.pm2p5);
	});

	/* handle click on menu observation, sensor and phot */
	$("#top-menu button").click(
			function(e) {
				$("#top-menu").find(".btn-warning").removeClass("btn-warning")
						.addClass("btn-default");
				$(this).addClass("btn-warning").removeClass("btn-default");

				$("form [connectedto]").addClass("hidden");
				$("[connectedto='" + this.id + "']").removeClass("hidden");
			});

	/* for file upload */
	$("#image-uploader-helper").click(function() {
		$("#image-upload").click();
	});
	if (window.File && window.FileReader && window.FileList && window.Blob) {
		function handleFileSelect(evt) {
			var files = evt.target.files; // FileList object
			for (var i = 0, f; f = files[i]; i++) {
				$("#image-uploader-helper").html(
						escape(f.name) + " (" + f.size + " bytes)");
			}
		}

		document.getElementById('image-upload').addEventListener('change',
				handleFileSelect, false);
	} else {
		alert('The File APIs are not fully supported in this browser.');
	}

	/* user observation */
	$("#user-observation button").click(
			function() {
				$("#user-observation").find("[data-toggle='yes']")
						.removeClass().addClass("btn btn-default");
				$(this).attr("data-toggle", "yes").removeClass().addClass(
						$(this).attr("data-active"));
				$(
						"#user-observation input[type='radio'][connectedTo='"
								+ this.id + "']").prop('checked', true);
			});

	$("#submit-button").click(function() {
		$("form").submit();
	});
}

/**
 * Called when page is loaded
 */
function initialize() {
	doFormThings();
	doMapThings();

	// file upload code over

}

google.maps.event.addDomListener(window, 'load', initialize);
